import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:provider/provider.dart';

import '../../../lib_baseview/lib_baseview.dart';
import '../doufeng/ride_util.dart';
import '../interface/room_page.dart';
import '../interface/root_proxy_model.dart';
import '../mixin/room_overlay_draggable_mixin.dart';

class RoomFloatingView extends StatefulWidget {
  const RoomFloatingView({Key? key}) : super(key: key);

  @override
  State<RoomFloatingView> createState() => _RoomFloatingViewState();
}

class _RoomFloatingViewState extends State<RoomFloatingView>
    with TickerProviderStateMixin<RoomFloatingView>, RoomOverlayDraggableMixin {
  late RootProxyModel roomModel;

  @override
  void initState() {
    super.initState();
    roomModel = context.read<RootProxyModel>();
  }

  // String get avatar => roomModel.channel?.icon ?? '';

  String get avatar => '';

  @override
  Widget get child => _FloatingWidget(
        avatar: avatar,
        rootModel: roomModel,
      );

  @override
  double get childHeight => 63;

  @override
  double get childWidth => 63;

  @override
  TickerProvider get tickerProvider => this;

  /// 是否显示房间悬浮窗
  /// 加入房间 & 房间页面没有打开 & 非青少年模式
  @override
  bool get show =>
      roomModel.inRoom && !RoomPage.exist /* && !MainPageModel.inTeen */;
}

class _FloatingWidget extends StatelessWidget {
  final String avatar;
  final RootProxyModel rootModel;

  const _FloatingWidget(
      {Key? key, required this.avatar, required this.rootModel})
      : super(key: key);

  /// 房主头像
  // Widget get avatarWidget => GradientBorder(
  //     borderGradient: LinearGradient(colors: JR.color.brandGradient),
  //     borderWidth: 2,
  //     borderRadius: 25,
  //     child: Container(
  //       height: 50,
  //       width: 50,
  //       alignment: AlignmentDirectional.center,
  //       child: AvatarImage(
  //           url: StringUtil.formatImgUrl(avatar, size: 250),
  //           width: 46,
  //           height: 46,
  //           borderRadius: 23),
  //     ));

  /// 音频标识
  Widget get audioLineWidget => Container(
        height: 50,
        width: 50,
        decoration: BoxDecoration(
            color: const Color(0x4C000000),
            borderRadius: BorderRadius.circular(25)),
        alignment: Alignment.center,
        child: image('anim_in_room.webp',
            width: 16, fit: BoxFit.fitWidth, color: Colors.white),
      );

  /// 房主头像 & 音频标识
  Widget buildAvatarWithAudioLine(BuildContext context) => Positioned(
      left: 0,
      bottom: 0,
      child: GestureDetector(
        behavior: HitTestBehavior.translucent,
        onTap: () async {
          // RoomPage.show(context, rootModel.channel?.channelId.toInt() ?? 0);
        },
        child: SizedBox(
          width: 50,
          height: 50,
          child: Stack(
            children: [
              // avatarWidget,
              Text('房主头像'),
              audioLineWidget,
            ],
          ),
        ),
      ));

  /// 关闭按钮
  Widget get closeBtn => Positioned(
      right: 0,
      top: 0,
      child: GestureDetector(
        behavior: HitTestBehavior.translucent,
        onTap: () async {
          var result = await RideUtil.checkEndRide(rootModel);

          if (result != true) return;

          // 用户在麦上，弹窗退房提示
          // if (rootModel.meWithSlot != null) {
          //   result = await JRDialog.showCommonDialog(
          //     context: rootNavigatorState.currentContext!,
          //     data: TextWithHorizontalBtnDialogData(
          //       title: T.current!.quit_room,
          //       desc: T.current!.quit_room_tips,
          //     ),
          //   );
          // }

          if (result != true) return;

          // rootModel.hideOverlay();
          await rootModel.release();
        },
        child: Container(
          padding: const EdgeInsets.symmetric(vertical: 6, horizontal: 4),
          child: Container(
            height: 16,
            width: 16,
            alignment: Alignment.center,
            decoration: BoxDecoration(
              color: const Color(0x7F000000),
              shape: BoxShape.circle,
              border: Border.all(color: Colors.white, width: 1),
            ),
            child: Image.asset(
              "assets/images/ic_cancel.png",
              width: 8,
              height: 8,
              fit: BoxFit.contain,
              color: Colors.white,
            ),
          ),
        ),
      ));

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: 63,
      height: 63,
      child: Stack(
        children: [
          buildAvatarWithAudioLine(context),
          closeBtn,
        ],
      ),
    );
  }
}
